package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* TakePhotoView
* */
class TakePhotoView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth
            strokeCap = Paint.Cap.SQUARE
            strokeJoin = Paint.Join.MITER
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_E84C1D)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy + (cy / 6 - cy / 3) / 2)
            mPath0.apply {
                moveTo(-cx * 5 / 6, cy * 5 / 6)
                lineTo(-cx * 5 / 6, -cy * 5 / 12)
                lineTo(-cx * 5 / 12, -cy * 5 / 12)
                lineTo(-cx * 5 / 24, -cy * 2 / 3)
                lineTo(cx * 5 / 24, -cy * 2 / 3)
                lineTo(cx * 5 / 12, -cy * 5 / 12)
                lineTo(cx * 5 / 6, -cy * 5 / 12)
                lineTo(cx * 5 / 6, cy * 5 / 6)
                close()
            }
            drawPath(mPath0, mPaint0)

            save()
            val radius = cx * 7 / 24
            translate(0F,(cy * 5 / 6 - radius - (cx * 5 / 12 - radius)) / 2)
            drawCircle(
                0F,
                0F,
                radius,
                mPaint0
            )
            restore()
        }
    }
}